package com.ftg.learn.chapter10.work03;

import lombok.Data;

import java.io.Serializable;
import java.util.*;

/**
 * 狗
 *
 * 常见的接口
 *  1 : Serializable 序列化接口,在jvm解析把这个类序列化(转换成为二进制流)
 *      序列化或反序列化;
 *  2 : Cloneable  允许对实例进行克隆，克隆会产生新的地址，属性是一样的
 *  3 : RandomAccess 随机访问
 *  功能性接口
 *  4 : Comparable接口  Comparator 比较器函数式接口
 * @author kangjx
 * @version 1.0
 * @date 2023/2/3 上午9:37
 */
@Data
public class Dog implements Serializable ,Cloneable, RandomAccess {

    private final long serverId = 1L;

    int age;

    /**
     * 比较 方法
     * @param o the object to be compared.
     * @return
     */
//    @Override
    public int compareTo(Object o) {
        Dog o1 = (Dog) o;
        return this.getAge() - o1.getAge();
    }



    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.setAge(23);
        Dog dog1 = new Dog();
        dog1.setAge(2);
        Dog dog2 = new Dog();
        dog2.setAge(29);
        Dog[] dogs = new Dog[3];
        dogs[0] = dog;
        dogs[1] = dog1;
        dogs[2] = dog2;


        System.out.println(dog.compareTo(dog1));

        //Arrays.sort 比较的时候要求我们的类实现Comparable接口
        //重写 compareTo
//        Arrays.sort(dogs);
//        Arrays.sort(dogs, new Comparator<Dog>() {
//            @Override
//            public int compare(Dog o1, Dog o2) {
//                return o1.getAge() - o2.getAge();
//            }
//        });
//        Arrays.sort(dogs,(x,y)->-(x.getAge() - y.getAge()));
        Arrays.sort(dogs,Dog::compareTo);
//        Arrays.sort(dogs,Comparator.comparingInt(Dog::getAge));

        System.out.println(Arrays.toString(dogs));

    }

}
